package user;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

import utils.TechresourceDAORemote;
import utils.TechticketDAORemote;
import utils.UserDAORemote;
import beans.UserPageBean;
import dtos.TechresourceDTO;
import dtos.TechticketDTO;
import dtos.TechticketHistoryDTO;
import dtos.UserDTO;

/**
 * 
 * @author andreicl Techticket for basic user
 */
@ManagedBean
@ViewScoped
public class NotificationBean implements Serializable {
	private static final long serialVersionUID = 6509012476768758436L;

	@EJB
	TechresourceDAORemote techResource;
	@EJB
	TechticketDAORemote ticket;
	@ManagedProperty("#{userPageBean}")
	UserPageBean upb;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	/**
	 * remove this user and replace with session user
	 */
	@EJB
	UserDAORemote user;
	private UserDTO userDTO;
	private int idDomain = -1;

	private String description, problemType;
	private int resourceId = -1;
	private String detail;
	private DataModel<TechticketHistoryDTO> ticketDataModel;
	private TechticketDTO techTicketDTO;
	private TechresourceDTO techResourceDTO;
	private List<TechticketHistoryDTO> history;
	private List<TechresourceDTO> resources;
	private TechticketHistoryDTO[] tickets;
	private String feedback;

	@PostConstruct
	public void init() {
		techTicketDTO = new TechticketDTO();
		techTicketDTO.setDate(new Date());
		// replace here
		userDTO = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");

		techTicketDTO.setIdUser(userDTO.getId());
		idDomain = userDTO.getIdDomain();
		refresh();
		if (getResources() != null) {
			techResourceDTO = getResources()[0];
			this.setDetail(bundle
					.getString("BU_createNotification_notificationTable_detail1")
					+ techResourceDTO.getName()
					+ bundle.getString("BU_createNotification_notificationTable_detail2")
					+ techResourceDTO.getIdnumber()
					+ bundle.getString("BU_createNotification_notificationTable_detail3")
					+ techResourceDTO.getIp()
					+ bundle.getString("BU_createNotification_notificationTable_detail4")
					+ techResourceDTO.getStatus());
		}
	}

	public void refresh() {

		getTechTicket();
		convertTickets();
		ticketDataModel = new ArrayDataModel<>(tickets);
		description = "";
		problemType = "";
		feedback = null;
	}

	/**
	 * @return List<TechticketHistoryDTO> get all the techTickets
	 */
	public List<TechticketHistoryDTO> getTechTicket() {
		return ticket.getTechticketHistoryDTO(userDTO.getId());
	}

	/**
	 * convert the ArrayList to Array
	 */
	public void convertTickets() {
		history = getTechTicket();
		tickets = history.toArray(new TechticketHistoryDTO[history.size()]);
	}

	/**
	 * convert the ArrayList to Array
	 */
	public TechresourceDTO[] getResources() {
		resources = techResource.getTechResourcesDTOByDomainId(idDomain);
		return resources.toArray(new TechresourceDTO[resources.size()]);
	}

	/**
	 * 
	 * @param event
	 * 
	 *            used in GUI for displaying details about the selected resource
	 */
	public void valueChange(ValueChangeEvent event) {
		techResourceDTO = (TechresourceDTO) techResource
				.getTechresourcesById((Integer) event.getNewValue());
		this.setDetail(bundle
				.getString("BU_createNotification_notificationTable_detail1")
				+ techResourceDTO.getName()
				+ bundle.getString("BU_createNotification_notificationTable_detail2")
				+ techResourceDTO.getIdnumber()
				+ bundle.getString("BU_createNotification_notificationTable_detail3")
				+ techResourceDTO.getIp()
				+ bundle.getString("BU_createNotification_notificationTable_detail4")
				+ techResourceDTO.getStatus());
		techTicketDTO.setIdTechresource(techResourceDTO.getId());
	}

	/**
	 * 
	 * @param event
	 *            used for inserting a new Teckticket in DB when a button is
	 *            pushed in GUI and reseting the input fields
	 */
	public void insertTechTicket(ActionEvent event) {
		techTicketDTO.setStatus("Pending");
		ticket.insertTechticket(techTicketDTO);
		refresh();
		feedback = bundle.getString("BU_notificationBean_feedback");
	}

	/**
	 * 
	 * @param event
	 *            used for inserting a new Teckticket draft in DB when a button
	 *            is pushed in GUI and reseting the input fields
	 */
	public void updateNotification(ActionEvent event) {
		if (Integer.parseInt(event.getComponent().getAttributes()
				.get("idTicket").toString()) != -1) {
			TechticketDTO tmp = ticket
					.getTechticket(Integer.parseInt(event.getComponent()
							.getAttributes().get("idTicket").toString()));
			tmp.setStatus("Pending");
			ticket.updateTechticket(tmp);

		} else {
			techTicketDTO.setStatus("Saved");
			ticket.insertTechticket(techTicketDTO);
			refresh();
			upb.setPage("viewNotificationHistory");
		}
		refresh();
	}

	/**
	 * 
	 * @param event
	 *            used for deleting a new Teckticket draft in DB when a button
	 *            is pushed in GUI and reseting the input fields
	 */
	public void deleteNotification(ActionEvent event) {
		if (Integer.parseInt(event.getComponent().getAttributes()
				.get("idTicket").toString()) != -1) {
			TechticketDTO tmp = ticket
					.getTechticket(Integer.parseInt(event.getComponent()
							.getAttributes().get("idTicket").toString()));
			ticket.deleteTechticker(tmp);
			upb.setPage("viewNotificationHistory");
		}
		refresh();

	}

	/**
	 * 
	 * @return DataModel<TechticketHistoryDTO> used for line numbering in
	 *         dataTable in GUI
	 */
	public DataModel<TechticketHistoryDTO> getTickets() {
		return ticketDataModel;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		techTicketDTO.setDescription(description);
		this.description = description;
	}

	public String getProblemType() {
		return problemType;
	}

	public void setProblemType(String problemType) {
		techTicketDTO.setProblemtype(problemType);
		this.problemType = problemType;
	}

	public UserPageBean getUpb() {
		return upb;
	}

	public void setUpb(UserPageBean upb) {
		this.upb = upb;
	}

	public String getFeedback() {
		return feedback;
	}

	public void setFeedback(String feedback) {
		this.feedback = feedback;
	}

	public void feedbackRefresh(ValueChangeEvent event) {
		feedback = null;
	}

}
